home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / symtable.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  13KB  |  307 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Interface to the compiler's internal symbol tables"""
  5. import _symtable
  6. from _symtable import USE, DEF_GLOBAL, DEF_LOCAL, DEF_PARAM, DEF_IMPORT, DEF_BOUND, OPT_IMPORT_STAR, OPT_EXEC, OPT_BARE_EXEC, SCOPE_OFF, SCOPE_MASK, FREE, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT
  7. import warnings
  8. import weakref
  9. __all__ = [
  10.     'symtable',
  11.     'SymbolTable',
  12.     'Class',
  13.     'Function',
  14.     'Symbol']
  15.  
  16. def symtable(code, filename, compile_type):
  17.     raw = _symtable.symtable(code, filename, compile_type)
  18.     for top in raw.itervalues():
  19.         if top.name == 'top':
  20.             break
  21.             continue
  22.     
  23.     return _newSymbolTable(top, filename)
  24.  
  25.  
  26. class SymbolTableFactory:
  27.     
  28.     def __init__(self):
  29.         self._SymbolTableFactory__memo = weakref.WeakValueDictionary()
  30.  
  31.     
  32.     def new(self, table, filename):
  33.         if table.type == _symtable.TYPE_FUNCTION:
  34.             return Function(table, filename)
  35.         if table.type == _symtable.TYPE_CLASS:
  36.             return Class(table, filename)
  37.         return SymbolTable(table, filename)
  38.  
  39.     
  40.     def __call__(self, table, filename):
  41.         key = (table, filename)
  42.         obj = self._SymbolTableFactory__memo.get(key, None)
  43.         if obj is None:
  44.             obj = self._SymbolTableFactory__memo[key] = self.new(table, filename)
  45.         
  46.         return obj
  47.  
  48.  
  49. _newSymbolTable = SymbolTableFactory()
  50.  
  51. class SymbolTable(object):
  52.     
  53.     def __init__(self, raw_table, filename):
  54.         self._table = raw_table
  55.         self._filename = filename
  56.         self._symbols = { }
  57.  
  58.     
  59.     def __repr__(self):
  60.         if self.__class__ == SymbolTable:
  61.             kind = ''
  62.         else:
  63.             kind = '%s ' % self.__class__.__name__
  64.         if self._table.name == 'global':
  65.             return '<{0}SymbolTable for module {1}>'.format(kind, self._filename)
  66.         return '<{0}SymbolTable for {1} in {2}>'.format(kind, self._table.name, self._filename)
  67.  
  68.     
  69.     def get_type(self):
  70.         if self._table.type == _symtable.TYPE_MODULE:
  71.             return 'module'
  72.         if self._table.type == _symtable.TYPE_FUNCTION:
  73.             return 'function'
  74.         if self._table.type == _symtable.TYPE_CLASS:
  75.             return 'class'
  76.         if not self._table.type in (1, 2, 3):
  77.             raise AssertionError, 'unexpected type: {0}'.format(self._table.type)
  78.  
  79.     
  80.     def get_id(self):
  81.         return self._table.id
  82.  
  83.     
  84.     def get_name(self):
  85.         return self._table.name
  86.  
  87.     
  88.     def get_lineno(self):
  89.         return self._table.lineno
  90.  
  91.     
  92.     def is_optimized(self):
  93.         if self._table.type == _symtable.TYPE_FUNCTION:
  94.             pass
  95.         return bool(not (self._table.optimized))
  96.  
  97.     
  98.     def is_nested(self):
  99.         return bool(self._table.nested)
  100.  
  101.     
  102.     def has_children(self):
  103.         return bool(self._table.children)
  104.  
  105.     
  106.     def has_exec(self):
  107.         '''Return true if the scope uses exec'''
  108.         return bool(self._table.optimized & (OPT_EXEC | OPT_BARE_EXEC))
  109.  
  110.     
  111.     def has_import_star(self):
  112.         '''Return true if the scope uses import *'''
  113.         return bool(self._table.optimized & OPT_IMPORT_STAR)
  114.  
  115.     
  116.     def get_identifiers(self):
  117.         return self._table.symbols.keys()
  118.  
  119.     
  120.     def lookup(self, name):
  121.         sym = self._symbols.get(name)
  122.         if sym is None:
  123.             flags = self._table.symbols[name]
  124.             namespaces = self._SymbolTable__check_children(name)
  125.             sym = self._symbols[name] = Symbol(name, flags, namespaces)
  126.         
  127.         return sym
  128.  
  129.     
  130.     def get_symbols(self):
  131.         return [ self.lookup(ident) for ident in self.get_identifiers() ]
  132.  
  133.     
  134.     def __check_children(self, name):
  135.         return _[1]
  136.  
  137.     
  138.     def get_children(self):
  139.         return [ _newSymbolTable(st, self._filename) for st in self._table.children ]
  140.  
  141.  
  142.  
  143. class Function(SymbolTable):
  144.     __params = None
  145.     __locals = None
  146.     __frees = None
  147.     __globals = None
  148.     
  149.     def __idents_matching(self, test_func):
  150.         return [](_[1])
  151.  
  152.     
  153.     def get_parameters(self):
  154.         if self._Function__params is None:
  155.             self._Function__params = self._Function__idents_matching((lambda x: x & DEF_PARAM))
  156.         
  157.         return self._Function__params
  158.  
  159.     
  160.     def get_locals(self):
  161.         if self._Function__locals is None:
  162.             self._Function__locals = self._Function__idents_matching((lambda x: x & DEF_BOUND))
  163.         
  164.         return self._Function__locals
  165.  
  166.     
  167.     def get_globals(self):
  168.         if self._Function__globals is None:
  169.             glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
  170.             
  171.             test = lambda x: x >> SCOPE_OFF & SCOPE_MASK in glob
  172.             self._Function__globals = self._Function__idents_matching(test)
  173.         
  174.         return self._Function__globals
  175.  
  176.     
  177.     def get_frees(self):
  178.         if self._Function__frees is None:
  179.             
  180.             is_free = lambda x: x >> SCOPE_OFF & SCOPE_MASK == FREE
  181.             self._Function__frees = self._Function__idents_matching(is_free)
  182.         
  183.         return self._Function__frees
  184.  
  185.  
  186.  
  187. class Class(SymbolTable):
  188.     __methods = None
  189.     
  190.     def get_methods(self):
  191.         if self._Class__methods is None:
  192.             d = { }
  193.             for st in self._table.children:
  194.                 d[st.name] = 1
  195.             
  196.             self._Class__methods = tuple(d)
  197.         
  198.         return self._Class__methods
  199.  
  200.  
  201.  
  202. class Symbol(object):
  203.     
  204.     def __init__(self, name, flags, namespaces = None):
  205.         self._Symbol__name = name
  206.         self._Symbol__flags = flags
  207.         self._Symbol__scope = flags >> SCOPE_OFF & SCOPE_MASK
  208.         if not namespaces:
  209.             pass
  210.         self._Symbol__namespaces = ()
  211.  
  212.     
  213.     def __repr__(self):
  214.         return '<symbol {0!r}>'.format(self._Symbol__name)
  215.  
  216.     
  217.     def get_name(self):
  218.         return self._Symbol__name
  219.  
  220.     
  221.     def is_referenced(self):
  222.         return bool(self._Symbol__flags & _symtable.USE)
  223.  
  224.     
  225.     def is_parameter(self):
  226.         return bool(self._Symbol__flags & DEF_PARAM)
  227.  
  228.     
  229.     def is_global(self):
  230.         return bool(self._Symbol__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT))
  231.  
  232.     
  233.     def is_vararg(self):
  234.         warnings.warn('is_vararg() is obsolete and will be removed', DeprecationWarning, 2)
  235.         return False
  236.  
  237.     
  238.     def is_keywordarg(self):
  239.         warnings.warn('is_keywordarg() is obsolete and will be removed', DeprecationWarning, 2)
  240.         return False
  241.  
  242.     
  243.     def is_declared_global(self):
  244.         return bool(self._Symbol__scope == GLOBAL_EXPLICIT)
  245.  
  246.     
  247.     def is_local(self):
  248.         return bool(self._Symbol__flags & DEF_BOUND)
  249.  
  250.     
  251.     def is_free(self):
  252.         return bool(self._Symbol__scope == FREE)
  253.  
  254.     
  255.     def is_imported(self):
  256.         return bool(self._Symbol__flags & DEF_IMPORT)
  257.  
  258.     
  259.     def is_assigned(self):
  260.         return bool(self._Symbol__flags & DEF_LOCAL)
  261.  
  262.     
  263.     def is_in_tuple(self):
  264.         warnings.warn('is_in_tuple() is obsolete and will be removed', DeprecationWarning, 2)
  265.  
  266.     
  267.     def is_namespace(self):
  268.         '''Returns true if name binding introduces new namespace.
  269.  
  270.         If the name is used as the target of a function or class
  271.         statement, this will be true.
  272.  
  273.         Note that a single name can be bound to multiple objects.  If
  274.         is_namespace() is true, the name may also be bound to other
  275.         objects, like an int or list, that does not introduce a new
  276.         namespace.
  277.         '''
  278.         return bool(self._Symbol__namespaces)
  279.  
  280.     
  281.     def get_namespaces(self):
  282.         '''Return a list of namespaces bound to this name'''
  283.         return self._Symbol__namespaces
  284.  
  285.     
  286.     def get_namespace(self):
  287.         '''Returns the single namespace bound to this name.
  288.  
  289.         Raises ValueError if the name is bound to multiple namespaces.
  290.         '''
  291.         if len(self._Symbol__namespaces) != 1:
  292.             raise ValueError, 'name is bound to multiple namespaces'
  293.         len(self._Symbol__namespaces) != 1
  294.         return self._Symbol__namespaces[0]
  295.  
  296.  
  297. if __name__ == '__main__':
  298.     import os
  299.     import sys
  300.     src = open(sys.argv[0]).read()
  301.     mod = symtable(src, os.path.split(sys.argv[0])[1], 'exec')
  302.     for ident in mod.get_identifiers():
  303.         info = mod.lookup(ident)
  304.         print info, info.is_local(), info.is_namespace()
  305.     
  306.  
  307.